package cn.itcast.web.controller.cargo;


import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.FinanceExample;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Date;


@Controller
@RequestMapping(value = "/cargo/finance", name = "财务模块")
public class FinanceController extends BaseController {

    @Reference
    private FinanceService financeService;
    @Reference
    private InvoiceService invoiceService;
    @RequestMapping(value = "/toFinance",name ="生成财务")
    public String tofinance(String id){
        Finance finance = new Finance();
        Invoice invoice = invoiceService.selectByPrimaryKey(id);
        invoice.setState(2);
        invoiceService.update(invoice);
        finance.setCreateBy(getLoginUser().getUserName());
        finance.setCreateDept(getLoginUser().getDeptName());
        finance.setCreateTime(new Date());
        finance.setInputBy(getLoginUser().getUserName());
        financeService.save(finance,id);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping(value = "/edit",name = "新增或修改")
    public String edit(Finance finance){
        if (StrUtil.isEmpty(finance.getFinanceId())) { // 新增报运单
        }else{ //更新报运单
            financeService.update(finance);
        }
        return "redirect:/cargo/finance/list.do";
    }


    @RequestMapping(value = "/list", name = "装箱单信息")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        FinanceExample financeExample = new FinanceExample();
        PageInfo page = financeService.findByPage(pageNum, pageSize, financeExample);
        request.setAttribute("page", page);
        return "cargo/finance/finance-list";
    }

    @RequestMapping(value = "delete",name = "删除装箱单")
    public String delete(String id){
        financeService.deleteByPrimaryKey(id);
        return "redirect:/cargo/finance/list.do";
    }


}
